<template>
  <page-header-wrapper>
    <a-card style="height: 85vh">
      <a-form-model ref="ruleFormValidated" :model="formValidated" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-form-model-item label="平台分红设置" prop="pool_share_switch">
          <a-radio-group v-model="formValidated.pool_share_switch" @change="switchVal">
            <a-radio value="1"> 开启 </a-radio>
            <a-radio value="0"> 关闭 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="购买商品分红比例" prop="pool_goods_rate">
          <a-input-number
            style="width: 60% !important"
            v-model="formValidated.pool_goods_rate"
            :min="0"
            :max="100"
            :step="1"
            :formatter="(value) => `${value}%`"
            :parser="(value) => value.replace('%', '')"
            suffix="%"
            :disabled="formValidated.pool_share_switch == '1' ? false : true"
          />
        </a-form-model-item>
        <a-form-model-item label="办理绑卡分红比例" prop="pool_card_rate">
          <a-input-number
            style="width: 60% !important"
            v-model="formValidated.pool_card_rate"
            :min="0"
            :max="100"
            :step="1"
            :formatter="(value) => `${value}%`"
            :parser="(value) => value.replace('%', '')"
            suffix="%"
            :disabled="formValidated.pool_share_switch == '1' ? false : true"
          />
        </a-form-model-item>
        <a-form-model-item label="话费充值分红比例" prop="pool_recharge_rate">
          <a-input-number
            style="width: 60% !important"
            v-model="formValidated.pool_recharge_rate"
            :min="0"
            :max="100"
            :step="1"
            :formatter="(value) => `${value}%`"
            :parser="(value) => value.replace('%', '')"
            suffix="%"
            :disabled="formValidated.pool_share_switch == '1' ? false : true"
          />
          <!-- <div class="tips">卡包月缴话费金额*该比例</div> -->
        </a-form-model-item>
        <a-form-model-item label="激励广告分红比例" prop="pool_ad_rate">
          <a-input-number
            style="width: 60% !important"
            v-model="formValidated.pool_ad_rate"
            :min="0"
            :max="100"
            :step="1"
            :formatter="(value) => `${value}%`"
            :parser="(value) => value.replace('%', '')"
            suffix="%"
            :disabled="formValidated.pool_share_switch == '1' ? false : true"
          />
        </a-form-model-item>
        <a-form-model-item>
          <a-button type="primary" :loading="loading" @click="saveSetting">保存</a-button>
        </a-form-model-item>
      </a-form-model>
    </a-card>
  </page-header-wrapper>
</template>

<script>
import { getPoolSetting, savePoolSetting } from '@/api/plat'
export default {
  name: 'pool-total-list-page',
  data() {
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      formValidated: {
        pool_share_switch: '1',
        pool_recharge_rate: 0,
        pool_ad_rate: 0,
        pool_goods_rate: 0,
        pool_card_rate: 0,
      },
      loading: false,
    }
  },
  created() {
    this.getSetting()
  },
  methods: {
    switchVal(e) {
      this.formValidated.pool_share_switch = e.target.value
    },
    getSetting() {
      getPoolSetting()
        .then((res) => {
          if (!res.code) {
            this.formValidated.pool_share_switch = res.data.pool_share_switch
            this.formValidated.pool_ad_rate = res.data.pool_ad_rate
            this.formValidated.pool_recharge_rate = res.data.pool_recharge_rate
            this.formValidated.pool_goods_rate = res.data.pool_goods_rate
            this.formValidated.pool_card_rate = res.data.pool_card_rate
          } else {
            this.$message.error(res.msg)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    saveSetting() {
      this.loading = true
      if (this.formValidated.pool_share_switch == '0') {
        this.formValidated.pool_recharge_rate = 0
        this.formValidated.pool_ad_rate = 0
        this.formValidated.pool_goods_rate = 0
        this.formValidated.pool_card_rate = 0
      }
      savePoolSetting(this.formValidated)
        .then((res) => {
          if (!res.code) {
            this.loading = false
            this.$message.success(res.msg)
            this.formValidated = {
              pool_share_switch: '1',
              pool_recharge_rate: 0,
              pool_ad_rate: 0,
              pool_goods_rate: 0,
              pool_card_rate: 0,
            }
            this.getSetting()
          } else {
            this.loading = false
            this.$message.error(res.msg)
          }
        })
        .catch((err) => {
          this.loading = false
          this.$message.error(err)
        })
    },
  },
}
</script>

<style lang="less" scoped>
</style>
